package com.hzit.page.service.impl;

import com.alibaba.fastjson2.JSON;
import com.hzit.cart.api.domain.Item;
import com.hzit.manager.api.RemoteManagerService;
import com.hzit.manager.api.domain.GoodsDesc;
import com.hzit.page.service.PageService;


import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.domain.AjaxResult;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
@Service
public class PageServiceImpl implements PageService {
	//0. 引入模板引擎
	@Autowired
	private TemplateEngine engine;
	@Autowired
	private RemoteManagerService managerService;

	//1. 根据商品id生成静态页面
	@Override
	public AjaxResult genPage(Long goodsId) throws IOException, InvocationTargetException, IllegalAccessException {
		//1.1 定义模板工作的上下文对象
		Context context = new Context();
		//1.2 定义存放上下文数据的集合
		Map<String, Object> data = new HashMap<>();
		//1.3 根据spu商品id查询spu
		AjaxResult info = managerService.getInfo(goodsId);
		if("500".equals(info.get("code") + "")){
			return info;
		}
		Map goods = (Map) info.get("data");
		// Map infoMap = (Map) info.get("data");
		data.put("goods",goods);
		//1.4 根据goodsId查询goodsDesc
		info = managerService.getDescInfo(goodsId);
		if("500".equals(info.get("code") + "")){
			return info;
		}
		GoodsDesc goodsDesc = new GoodsDesc();
		BeanUtils.copyProperties(goodsDesc,info.get("data"));
		data.put("goodsDesc",goodsDesc);
		//1.5 获取自定义扩展属性、规格列表、图片列表
		// List<Map> customAttributeItems = JSON.parseArray(goodsDesc.get("customAttributeItems") + "", Map.class);
		// List<Map> itemImages = JSON.parseArray(goodsDesc.get("itemImages") + "", Map.class);
		// List<Map> specificationItems = JSON.parseArray(goodsDesc.get("specificationItems") + "", Map.class);
		List<Map> customAttributeItems = JSON.parseArray(goodsDesc.getCustomAttributeItems(), Map.class);
		List<Map> itemImages = JSON.parseArray(goodsDesc.getItemImages(), Map.class);
		List<Map> specificationItems = JSON.parseArray(goodsDesc.getSpecificationItems(), Map.class);
		data.put("customAttributeItems",customAttributeItems);
		data.put("itemImages",itemImages);
		data.put("specificationItems",specificationItems);
		//1.6 根据goodsId查询出sku商品列表(tb_item)
		R<List<Item>> itemsByGoodsId = managerService.findItemsByGoodsId(goodsId);
		if (itemsByGoodsId.getCode() != 200) {
			return AjaxResult.error(itemsByGoodsId.getCode(),itemsByGoodsId.getMsg());
		}
		List<Item> itemList = itemsByGoodsId.getData();
		//1.7 取得默认值=1的sku商品
		Item defaultItem = null ;
		for (Item item : itemList) {
			if (item.getIsDefault().equals("1")) {
				defaultItem = item;
			}
		}
		//1.8 将sku商品列表及当前默认的sku商品对象添加到数据集合中
		data.put("itemList",itemList);
		data.put("item",defaultItem);
		//1.9 将集合数据绑定到context上下文环境中
		context.setVariables(data);
		//1.10 模板名称(逻辑视图名字)
		String template = "item";
		//1.11 定义输出流，指定输出生成静态页面
		FileWriter writer = new FileWriter("D:\\nginx-1.25.1\\html\\item\\" + goodsId + ".html");
		//1.12 执行模板引擎
		engine.process(template,context,writer);

		return AjaxResult.success();
	}
}
